package com.woniuxy.oa.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.PersonnelPageBean;
import com.woniuxy.oa.entity.Personnel;
import com.woniuxy.oa.mapper.EmployeeMapper;
import com.woniuxy.oa.mapper.PersonnelMapper;
import com.woniuxy.oa.service.PersonnelService;
import com.woniuxy.oa.utils.CurrentDateUtils;
@Service("personnelService")
public class PersonnelServiceImpl implements PersonnelService{
	@Autowired
	PersonnelMapper personnelMapper;
	@Autowired
	EmployeeMapper employeeMapper;
	
	@Transactional(rollbackFor=java.lang.Exception.class)
	@Override
	public void addPersonnel(Personnel personnel) {
		Employee employee = personnel.getEmployee();
		employeeMapper.insertEmployee(employee);
		personnelMapper.insertPersonnel(personnel);
	}
	
	@Transactional(rollbackFor=java.lang.Exception.class)
	@Override
	public void removePersonnelByPid(Integer p_id,Integer id) {
		personnelMapper.deletePersonnelByPid(p_id);
		employeeMapper.deleteEmployeeById(id);
	}
	
	@Transactional(rollbackFor=java.lang.Exception.class)
	@Override
	public void modifyPersonnel(Personnel personnel) {
		personnel.setPupdatetime(CurrentDateUtils.getCurrentDate());
		personnelMapper.updatePersonnel(personnel);
		employeeMapper.updateEmployee(personnel.getEmployee());
	}
	
	@Override
	public PersonnelPageBean<Personnel> getPageBeanByMultiCondition(PersonnelPageBean<Personnel> pageBean,
			String beginDate, String endDate) {
		int totalRecord = (int) personnelMapper.selectAllRecordsCountByMultiCondition(pageBean, beginDate, endDate);
		pageBean.setTotalRecord(totalRecord);
		pageBean.setTotalPage();
		Integer totalPage = pageBean.getTotalPage();
		if(totalPage > 0 && pageBean.getCurrentPage() > totalPage) {
			pageBean.setCurrentPage(totalPage);
		}
		int size = pageBean.getPageSize();
		int start = (pageBean.getCurrentPage()-1)*size;
		List<Personnel> beans = personnelMapper.selectPersonnelsByMultiConditionAndPage(pageBean, beginDate, endDate, start, size);
		pageBean.setBeanList(beans);
		return pageBean;
	}

	@Override
	public Personnel getPersonnelByPid(Integer p_id) {
		return personnelMapper.selectPersonnelByPid(p_id);
	}



}
